package de.zainodis.triplog.data.persistence;

import android.content.Context;

import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTableConfig;

import java.sql.SQLException;

import de.zainodis.triplog.data.DiaryEntry;

/**
 * Created by fzarrai on 9/9/2014.
 */
public interface IDiaryDao extends Dao<DiaryEntry, Long> {

    public static final String TABLE_NAME = "diary";
    //Forein key of the location table
    public static final String COLUMN_FK_TRIP_ID = "trip_id";
    public static final String COLUMN_ID = "_id";
    //Diary content
    public static final String COLUMN_TEXT = "text";
    //Date of diary entry
    public static final String COLUMN_DATE = "date";

    long createIfNotExists(Context context, DiaryEntry note) throws SQLException;

    public class DiaryDao extends BaseDaoImpl<DiaryEntry, Long> implements IDiaryDao {
        private static final String TAG = "DiaryDao";

        public DiaryDao(ConnectionSource connectionSource) throws SQLException {
            super(connectionSource, DiaryEntry.class);
        }

        public DiaryDao(ConnectionSource connectionSource, DatabaseTableConfig<DiaryEntry> tableConfig)
                throws SQLException {
            super(connectionSource, tableConfig);
        }

        @Override
        public long createIfNotExists(Context context, DiaryEntry note) throws SQLException {
            DiaryEntry entry = super.createIfNotExists(note);

            if (entry == null) {
                throw new SQLException("Failed to insert note - returned note is null.");
            }
            return entry.getId();
        }
    }
}
